﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using University.Workshop.Model;
using University.Workshop.DataAccess;
using System.Data;

[assembly: CLSCompliant(true)]
namespace University.Workshop.Business
{
    public static class UserController
    {
        public static string InsertUser(string userId, string userName, string userSurname1, string userSurname2, string userTelephone, string userCellPhone, string userAddress, string userPassword, string reUserPassword, string userOccupation)
        {
            User user = new User();
            if (string.IsNullOrEmpty(userId) || string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(userSurname1) || string.IsNullOrEmpty(userSurname2) || string.IsNullOrEmpty(userPassword))
            {
                return "Faltan llenar campos obligatorios, revise e ingrese, por favor!";
            }
            else
            {
                if (reUserPassword == userPassword)
                {
                    if (CheckIfExistsSameId(userId))
                    {
                        user.Id = userId;
                        user.Name = userName;
                        user.Surname1 = userSurname1;
                        user.Surname2 = userSurname2;
                        user.Telephone = userTelephone;
                        user.CellPhone = userCellPhone;
                        user.Address = userAddress;
                        user.Password = userPassword;
                        user.Occupation = userOccupation;
                        user.State = "A";
                        UserManagerDataAccess.InsertUser(user);
                        return "Usuario almacenado!";
                    }
                    else
                        return "El CI ingresado ya existe, verifique en la lista de usuarios o re-ingrese el campo!";
                }
                else
                    return "Contraseñas no coinciden!";
            }
        }

        public static bool CheckIfExistsSameId(string id)
        {
            return UserManagerDataAccess.CheckIfExistsSameId(id);
        }

        public static bool CheckIfPasswordIsCorrect(string password,string id)
        {
            return UserManagerDataAccess.CheckIfPasswordIsCorrect(password, id);
        }

        public static string UpdateUserPassword(string id, string password,string rePassword, string oldPassword)
        {
            if (CheckIfPasswordIsCorrect(oldPassword, id))
            {
                if (password == rePassword && !string.IsNullOrEmpty(password))
                {
                    UserManagerDataAccess.UpdatePassword(id, password);
                    return "La modificacion de contraseña ha sido exitosa!";
                }
                else
                    return "La contraseña nueva no coincide.";
            }
            else
                return "La contraseña antigua es incorrecta.";
        }

        public static string UpdateUserData(string userId, string userName, string userSurname1, string userSurname2, string userTelephone, string userCellPhone, string userAddress, string userOccupation)
        {
            User user = new User();
            if (string.IsNullOrEmpty(userId) || string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(userSurname1) || string.IsNullOrEmpty(userSurname2))
            {
                return "Faltan llenar campos obligatorios, revise e ingrese.";
            }
            else
            {
                user.Id = userId;
                user.Name = userName;
                user.Surname1 = userSurname1;
                user.Surname2 = userSurname2;
                user.Telephone = userTelephone;
                user.CellPhone = userCellPhone;
                user.Address = userAddress;
                user.Occupation = userOccupation;
                UserManagerDataAccess.UpdateUserData(user);
                return "Usuario modificado!";
            }
        }

        public static DataTable SearchAUser(string id, string name, string surname1, string surname2)
        {
            return UserManagerDataAccess.SearchAUser(id, name, surname1, surname2);
        }

        public static bool DeleteUser(string value)
        {
            return UserManagerDataAccess.DeleteUserData(value);
        }
    }
}
